#include "../header/net/net_info.h"

void  get_net_rate(long* rev,long* send){
    FILE* fp;
    fp = fopen("/proc/net/dev","r");
    int i = 0;
    long *rev_tmp,*send_tmp;
    rev_tmp = rev;
    send_tmp = send;
    if(fp != NULL){
        char buff[1024];
        char tempstr[16][16] = {"0"};
        while(fgets(buff,1024,fp)){
            ++i;
            if(i<3) continue;
            sscanf(buff,"%s\t%[^' ']\t%[^' ']\t%[^' ']\t%[^' ']\t%[^' ']\t%[^' ']\t%[^' ']\t%[^' ']\t%[^' ']\t%[^' ']\t%[^' ']\t",tempstr[0],tempstr[1],tempstr[2],tempstr[3],tempstr[4],tempstr[5],tempstr[6],tempstr[7],tempstr[8],tempstr[9],tempstr[10],tempstr[11]);
            //printf("tempstr[1]:%s,tempstr[1]:%s,tempstr[9]:%s\n",tempstr[0],tempstr[1],tempstr[9]);
            *rev += atol(tempstr[1]);//接受数据
            *send += atol(tempstr[9]);//发送数据
            //printf("rev:%ld,%ld\n",*rev,*send);
        }


     fclose(fp);
    }
}


cJSON * get_net_connections(long* conn_count){
    FILE* fp;
    int tcp_size,index = 0;
    char sl[10];
    tcp_size = sizeof(Tcp_detail);
    fp = fopen("/proc/net/tcp","r");
    cJSON *tcp_connection_infos = cJSON_CreateArray();
    if(fp != NULL){
        char buff[TCP_MAX_LEN];
        while(fgets(buff,TCP_MAX_LEN,fp)){
            if(index==0){
                ++index;
            }else{
                Tcp_detail tcp;
                sscanf(buff,"%s %[^:]:%[^: ]%[^:]:%[^: ] %s %[^:]:%[^: ]%[^:]:%[^: ] %s %s %s %s %s %s %s %s %s %s %s %s",
                       sl,tcp.local_ip,tcp.local_port,tcp.remote_ip,tcp.remote_port,tcp.st,tcp.transmit_queue,tcp.receive_queue,
                       tcp.tcp_timer,tcp.timer_expires,tcp.timeout_resend_times,tcp.uid,tcp.timeout,tcp.inode,tcp.socket_times,tcp.socket_mem_addr,tcp.RTO,tcp.delay,tcp.quick_flag,tcp.win_size,tcp.threshold);
                cJSON * tcp_json = cJSON_CreateObject();
                cJSON_AddStringToObject(tcp_json,"local_ip",tcp.local_ip);
                cJSON_AddStringToObject(tcp_json,"local_port",tcp.local_port);
                cJSON_AddStringToObject(tcp_json,"remote_ip",tcp.remote_ip);
                cJSON_AddStringToObject(tcp_json,"remote_port",tcp.remote_port);
                cJSON_AddStringToObject(tcp_json,"st",tcp.st);
                cJSON_AddStringToObject(tcp_json,"transmit_queue",tcp.transmit_queue);
                cJSON_AddStringToObject(tcp_json,"receive_queue",tcp.receive_queue);
                cJSON_AddStringToObject(tcp_json,"tcp_timer",tcp.tcp_timer);
                cJSON_AddStringToObject(tcp_json,"timer_expires",tcp.timer_expires);
                cJSON_AddStringToObject(tcp_json,"timeout_resend_times",tcp.timeout_resend_times);
                cJSON_AddStringToObject(tcp_json,"uid",tcp.uid);
                cJSON_AddStringToObject(tcp_json,"timeout",tcp.timeout);
                cJSON_AddStringToObject(tcp_json,"inode",tcp.inode);
                cJSON_AddStringToObject(tcp_json,"socket_times",tcp.socket_times);
                cJSON_AddStringToObject(tcp_json,"socket_mem_addr",tcp.socket_mem_addr);
                cJSON_AddStringToObject(tcp_json,"RTO",tcp.RTO);
                cJSON_AddStringToObject(tcp_json,"delay",tcp.delay);
                cJSON_AddStringToObject(tcp_json,"quick_flag",tcp.quick_flag);
                cJSON_AddStringToObject(tcp_json,"win_size",tcp.win_size);
                cJSON_AddStringToObject(tcp_json,"threshold",tcp.threshold);
                cJSON_AddItemToArray(tcp_connection_infos,tcp_json);
                ++index;
            }
        }
      }
    if(index!=0) *conn_count = (index-1);
    fclose(fp);
    return tcp_connection_infos;
}





Tcp_detail* get_net_connections_infos(long* conn_count){
    FILE* fp;
    int tcp_size,index = 0;
    char sl[10];
    tcp_size = sizeof(Tcp_detail);
    fp = fopen("/proc/net/tcp","r");
    Tcp_detail* tcp_detail;
    tcp_detail = (Tcp_detail*)malloc(tcp_size);
    if(fp != NULL){
        char buff[TCP_MAX_LEN];
        while(fgets(buff,TCP_MAX_LEN,fp)){
            if(index==0){
                ++index;
            }else{
                tcp_detail = (Tcp_detail*)realloc(tcp_detail,tcp_size*index);
                Tcp_detail tcp;
                sscanf(buff,"%s %[^:]:%[^: ]%[^:]:%[^: ] %s %[^:]:%[^: ]%[^:]:%[^: ] %s %s %s %s %s %s %s %s %s %s %s %s",
                       sl,tcp.local_ip,tcp.local_port,tcp.remote_ip,tcp.remote_port,tcp.st,tcp.transmit_queue,tcp.receive_queue,
                       tcp.tcp_timer,tcp.timer_expires,tcp.timeout_resend_times,tcp.uid,tcp.timeout,tcp.inode,tcp.socket_times,tcp.socket_mem_addr,tcp.RTO,tcp.delay,tcp.quick_flag,tcp.win_size,tcp.threshold);
                tcp_detail[index-1] = tcp;
                ++index;
            }
        }
    }
    if(index!=0) *conn_count = (index-1);
    fclose(fp);
    return tcp_detail;
}
